﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for events data manipulation
    /// </summary>
    public class EventsRepository : BaseRepository<Event>, IEventsRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EventsRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public EventsRepository(IDataServiceContext<Event> dataServiceContext)
            :base(dataServiceContext)
        {

        }

        /// <summary>
        /// Gets the events by group.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        /// <returns>The events of a group.</returns>
        public List<Event> GetEventsByGroup(string groupID)
        {
            return (from lcaEvent in Entities
                    where lcaEvent.Group == groupID
                    select lcaEvent).ToList();
        }

        /// <summary>
        /// Gets the next group events.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        /// <param name="quantity">The quantity.</param>
        /// <returns>The next events of the group.</returns>
        public List<Event> GetNextGroupEvents(string groupID, int quantity)
        {
            return (from lcaEvent in Entities
                    where lcaEvent.Group == groupID && lcaEvent.Date >= DateTime.Now
                    orderby lcaEvent.Date 
                    descending
                    select lcaEvent).Take(quantity).ToList();
        }


        /// <summary>
        /// Deletes all groups events.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        public void DeleteAllGroupsEvents(string groupID)
        {
            var events = GetEventsByGroup(groupID);

            events.ForEach(e => Delete(e));
        }

        /// <summary>
        /// Gets the recent events.
        /// </summary>
        /// <value>The recent events.</value>
        public List<Event> RecentEvents
        {
            get 
            {
                return Entities.OrderByDescending(u => u.Timestamp).Take(10).ToList();
            }
        }
    }
}
